<?php
/**
 * Created by PhpStorm.
 * User: chuyun
 * Date: 2018/8/14
 * Time: 9:58
 */

namespace App\Controllers;


use App\Models\Access;
use App\Models\Role;
use Interop\Container\ContainerInterface;
use Slim\Http\Request;
use Respect\Validation\Validator as v;

class RoleController extends BaseController
{
    public function __construct(ContainerInterface $container)
    {
        parent::__construct($container);
    }

    public function getOptionList(Request $request){
        $list = Role::select('id','title')->get()->toArray();
        return $this->jsonWithData(1,'OK',$list);

    }

    public function getRoleList() {
        $list   = $this->getRoleLoop(0);
        return $this->jsonWithData(1,'OK',$list);
    }

    public function roleStore(Request $request) {
        $params = $request->getParsedBody();
        //校验
        $validate =  $this->container->validator->validateArray($params,
            [
                'name'  => v::noWhitespace()->notEmpty()->existsInTable($this->container->db->table('role'), 'name')->setTemplate('同名的角色组已存在或是角色组名为空'),
                'title' => v::noWhitespace()->notEmpty()->setTemplate('请输入角色名称')
            ]);
        if($validate->failed()){
            $errors = $validate->getErrors();
            foreach ($errors as $item){
                $message = $item[0];
                break;
            }
            return $this->json(1005,$message);
        }
        $role = new Role();
        $role->name   = $params['name'];
        $role->title  = $params['title'];
        $role->pid    = isset($params['pid'])&&!empty($params['pid'])?$params['pid']:0;
        if($role->save()){
            return $this->json(1,'OK');
        }else{
            return $this->json(1,'创建失败');
        }
    }

    public function roleDelete(Request $request) {
        $params = $request->getParsedBody();
        if(intval($params['id'])==0){
            return $this->json(1005,'id不合法');
        }
        $tagcount = Role::where('pid',$params['id'])->count();
        if($tagcount){
            return $this->json(0,'该权限组组存在子权限组，不允许删除！');
        }

        $adCount = Access::where('role_id',$params['id'])->count();
        if($adCount){
            return $this->json(0,'该权限组已使用，不允许删除！');
        }
        $ret = Role::where('id',$params['id'])->delete();
        if($ret){
            return $this->json(1,'OK');
        } else {
            return $this->json(0,'删除失败');
        }
    }

    public function getRoleLoop($id) {
        $roleList = Role::where(['pid'=>$id])->get();
        $list     = [];
        $i        = 0;
        foreach ($roleList as $key => $val){
            $list[$i]['id']     = $val->id;
            $list[$i]['name']   = $val->name;
            $list[$i]['title']  = $val->title;
            $list[$i]['status'] = $val->status;
            $list[$i]['pid']    = $val->pid;
            $children = $this->getRoleLoop($val->id);
            if($children){
                $list[$i]['children'] = $children;
            }
            $i++;
        }
        return $list;
    }
}